﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Schedule
{
    public partial class Scheduledefault : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.IsInRole("admin"))
            {
                ConflictGridView.Columns[0].Visible = false;
                ScheduleGridView.Columns[0].Visible = false;
                ScheduleGridView.Columns[1].Visible = false;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton scheduleMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("ScheduleMenuButton");
            scheduleMenuButton.CssClass = "selected";

            if (HttpContext.Current.Session["CurrentDepartmentId"] != null)
            {
                int CurrentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                cs499.Database.Department CurrentDepartment = cs499.Database.ListOfDepartment.GetDepartment(ID: CurrentDepartmentId);
                DepartmentLabel.Text = CurrentDepartment.Name;

            }
            else
            {
                DepartmentLabel.Text = "(No departments defined)";
            }
        }

        protected void generateschedulebutton_Click(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.IsInRole("admin")) return;

            Department currentdepartment = ListOfDepartment.GetDepartment(Name: Master.DepartmentSelection.SelectedValue);
            if (currentdepartment.Schedule != null)
            {
                currentdepartment.Schedule.Delete();
                currentdepartment = ListOfDepartment.GetDepartment(Name: Master.DepartmentSelection.SelectedValue);
            }
            Model1Container db = new Model1Container();
            Database.Schedule newSchedule = currentdepartment.GenerateSchedule(db);

            
            foreach (CourseInstance ci in newSchedule.ScheduledCourses)
            {
                db.Set<CourseInstance>().Add(ci);
            }
            foreach (CourseInstance ci in newSchedule.ConflictedCourses)
            {
                db.Set<CourseInstance>().Add(ci);
            }
            db.Set<Database.Schedule>().Add(newSchedule);
            db.SaveChanges();

            ConflictGridView.DataBind();
            ScheduleGridView.DataBind();
        }

        protected void UnscheduleButton_Click(object sender, EventArgs e)
        {
            GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;

            Model1Container db = new Model1Container();
            int currentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
            Database.Schedule schedule = Department.GetSchedule(currentDepartmentId);
            int courseInstanceId = schedule.ScheduledCourses.ToList()[clickedRow.DataItemIndex].Id;
            CourseInstance courseInstance = db.Set<CourseInstance>().Include("Course").Include("TaughtBy").Include("Room").Include("TimeSlot").Include("Schedule").FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);

            courseInstance.TaughtBy = null;
            courseInstance.Room = null;
            courseInstance.TimeSlot = null;
            courseInstance.ScheduleIdforConflicted = schedule.Id;
            courseInstance.ScheduleIdforScheduled = null;

            db.SaveChanges();

            ConflictGridView.DataBind();
            ScheduleGridView.DataBind();
        }

        protected void deleteschedulebutton_Click(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.IsInRole("admin")) return;

            Department currentdepartment = ListOfDepartment.GetDepartment(Name: Master.DepartmentSelection.SelectedValue);
            if (currentdepartment.Schedule != null)
            {
                currentdepartment.Schedule.Delete();

                ConflictGridView.DataBind();
                ScheduleGridView.DataBind();
            }
        }
    }
}